/* --------------------------- resourcemanager.js --------------------------- */
VED.resourceManager = new Class({
   initialize: function( basePath, fileName ) {
      this.basePath = basePath;
      this.fileName = fileName;
      this.registered = {};
      this.loaded = {};
      this.externals = {};
      this._ = {
         // List of callbacks waiting for plugins to be loaded.
         waitingList: {}
      };
   },
   
   add: function( name, definition ) {
      if( this.registered[ name ] )
         throw '[VED.resourceManager.add] The resource name "' + name + '" is already registered.';

      var resource = this.registered[ name ] = definition || {};
      resource.name = name;
      resource.path = this.getPath( name );

      VED.fire( name + VED.tools.capitalize( this.fileName ) + 'Ready', resource );

      return this.get( name );
   },

   get: function( name ) {
      return this.registered[ name ] || null;
   },

   getPath: function( name ) {
      var external = this.externals[ name ];
      return VED.getUrl( ( external && external.dir ) || this.basePath + name + '/' );
   },

   getFilePath: function( name ) {
      var external = this.externals[ name ];
      return VED.getUrl( this.getPath( name ) + ( ( external && ( typeof external.file == 'string' ) ) ? external.file : this.fileName + '.js' ) );
   },

   addExternal: function( names, path, fileName ) {
      names = names.split( ',' );
      for ( var i = 0; i < names.length; i++ ) {
         var name = names[ i ];

         this.externals[ name ] = {
            dir: path,
            file: fileName
         };
      }
   },

   load: function( names, callback, scope ) {
      // Ensure that we have an array of names.
      if( !VED.tools.isArray( names ) )
         names = names ? [ names ] : [];

      var loaded = this.loaded,
         registered = this.registered,
         urls = [],
         urlsNames = {},
         resources = {};

      // Loop through all names.
      for ( var i = 0; i < names.length; i++ ) {
         var name = names[ i ];

         if( !name )
            continue;

         // If not available yet.
         if( !loaded[ name ] && !registered[ name ] ) {
            var url = this.getFilePath( name );
            urls.push( url );
            if( !( url in urlsNames ) )
               urlsNames[ url ] = [];
            urlsNames[ url ].push( name );
         } else
            resources[ name ] = this.get( name );
      }

      VED.scriptLoader.load( urls, function( completed, failed ) {
         if( failed.length ) {
            throw '[VED.resourceManager.load] Resource name "' + urlsNames[ failed[ 0 ] ].join( ',' )
               + '" was not found at "' + failed[ 0 ] + '".';
         }

         for ( var i = 0; i < completed.length; i++ ) {
            var nameList = urlsNames[ completed[ i ] ];
            for ( var j = 0; j < nameList.length; j++ ) {
               var name = nameList[ j ];
               resources[ name ] = this.get( name );

               loaded[ name ] = 1;
            }
         }

         callback.call( scope, resources );
      }, this );

   }
});





